package org.dfzt.modules.system.controller;

import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.dfzt.common.api.vo.Result;
import org.dfzt.common.constant.CommonConstant;
import org.dfzt.common.system.service.ISysBaseAPI;
import org.dfzt.common.system.vo.LoginUser;
import org.dfzt.common.util.VerifyParam;
import org.dfzt.modules.system.entity.SysUserConfig;
import org.dfzt.modules.system.service.ISysUserConfigService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 用户登录参数设置表
 * @Date:   2020-06-29
 * @Version: V1.0
 */

@Api(tags="用户登录参数设置表")
@RestController
@RequestMapping("/sys/sysUserConfig")
public class SysUserConfigController {
	private static final Log log = LogFactory.getLog(SysUserConfigController.class);
	@Resource
	private ISysUserConfigService sysUserConfigService;
	@Resource
	private ISysBaseAPI sysBaseAPI;

	@RequestMapping(value = "/getSettings", method = RequestMethod.GET)
	public Result<SysUserConfig> getSettings() {
		Result<SysUserConfig> result = new Result<>();
		SysUserConfig sysUserConfig = sysUserConfigService.getById(1);
		result.setResult(sysUserConfig);
		return result;
	}


	@RequestMapping(value = "/editSettings", method = RequestMethod.POST)
	//@RequiresRoles({"admin"})
	public Result<SysUserConfig> editSettings(@RequestBody SysUserConfig sysUserConfig) {
		Result<SysUserConfig> result = new Result<>();

		if(!VerifyParam.verifyParam("SysUserConfig",sysUserConfig,"object")){
			result.error500("数据范围验证失败！");
			return result;
		}
		SysUserConfig byId = sysUserConfigService.getById(sysUserConfig.getId());
		if(byId==null) {
			result.error500("未找到对应实体");
			sysBaseAPI.addLog("户登录设置更新失败", CommonConstant.TYPE_SYSTEM, CommonConstant.OPERATE_TYPE_UPDATE, CommonConstant.FLAG_FAIL, null);
		}else {
			sysUserConfig.setUpdateTime(new Date());
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(sysUser!=null){
				sysUserConfig.setUpdateBy(sysUser.getUsername());
			}
			boolean ok = sysUserConfigService.updateById(sysUserConfig);
			if(ok) {
				sysUserConfigService.initUserConfig();
				result.success("修改成功!");
				sysBaseAPI.addLog("用户登录设置更新成功", CommonConstant.TYPE_SYSTEM, CommonConstant.OPERATE_TYPE_UPDATE, CommonConstant.FLAG_SUCCESS, null);
			}
		}
		return result;
	}

}
